/**
 * 更多游戏
 * Created by duwei on 2019/1/8.
 */
var MoreGameWindow = Window.extend({
    _ccsFileName: res.MoreGameWindow_json,
    _beginPos:cc.p(355,425),
    _count:4,
    _winth:250,
    _hegth:210,
    tabBtnTpyes: {
        poker: 0,
        majiang: 1,
        yule: 2
    },
    tabBtnMaps:{
        0: ['btn_jbc_pdk','btn_jbc_ddz'],//扑克
        1: ['btn_jbc_xl'],//麻将
        2: ['btn_web_lcby','btn_web_rxby','btn_web_jsmr','btn_web_hztx','btn_web_bjlm']//联运
    },
    _btnsList: [
        ['_clickSub_jbc_ddz', 'btn_jbc_ddz'],
        ['_clickSub_jbc_pdk', 'btn_jbc_pdk'],
        ['_clickSub_jbc_xl', 'btn_jbc_xl'],

        ['_clickSub_web_meiren', 'btn_web_jsmr'],
        ['_clickSub_web_longcheng', 'btn_web_lcby'],
        ['_clickSub_web_rxby', 'btn_web_rxby'],
    ],
    initWindow:function(){
        this._initTabBtnsNode();
    },
    _initTabBtnsNode:function () {
        var self = this;
        self._tabBtnsNode = self._nodeList['root.tabBg'];
        for (var i = 0; i < self._tabBtnsNode.getChildrenCount(); i++) {
            (function (i) {
                var btn_tab = self._tabBtnsNode["btn_" + i];
                if (btn_tab) {
                    btn_tab.setTag(i);
                    TouchUtil.setOnclickListener(btn_tab, function () {
                        self._refreshTabBtn(i);
                    });
                }
            })(i)
        }
        self._refreshTabBtn(0);
    },
    /**
     * 刷新页签
     * @param type
     */
    _refreshTabBtn: function (type) {
        var self = this;
        if (self.tabBtnType == type)
            return;
        if(self._tabBtnsNode){
            for (var i = 0; i < self._tabBtnsNode.getChildrenCount(); i++) {
                var btn_tab = self._tabBtnsNode["btn_" + i];
                btn_tab["Image_1"].setVisible(btn_tab.getTag() == type);
            }
        }
        // 玩法按钮列表
        self.tabBtnType = type;
        self.refreshBtnMap();
    },
    refreshBtnMap:function () {
        var self = this;
        self.gameLayer = this._nodeList['root.gameLayer'];
        self.tabBtnMap = self.tabBtnMaps[self.tabBtnType];
        cc.log(self.tabBtnMap);
        var children = self.gameLayer.getChildren();
        for (var i = 0; i < children.length; i++) {
            var btn = children[i];
            var index = self.tabBtnMap.indexOf(btn.getName());
            btn.setVisible(index>=0);
            if(index>=0){
                var posx = index%4 *self._winth;
                var posy = parseInt(index/4) *self._hegth;
                btn.setPosition(cc.p(self._beginPos.x + posx,self._beginPos.y - posy));
            }
        }
    },
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {};
        for (var i = 0; i < this._btnsList.length; i++) {
            var _name = 'root.gameLayer.' +  this._btnsList[i][1];
            var btn = this._nodeList[_name];
            if (btn) {
                btn.setTag(i);
                this._clickListeners[_name] = '_clickButton';
            }
        }
    },
    /**
     * 点击按钮
     * @private
     */
    _clickButton: function (btn) {
        var tag = btn.getTag();
        var param = this._btnsList[tag];
        if (!param) return;
        //点击
        var _click = param[0];
        if (_click && _click.length > 0) {
            window.eventManager.dispatchEvent(EventKey.HallBoardRoomBtnEvent, {click:_click});
            this.close();
        }
    }
});